package initialize

import (
	"a_plus_station/global"
	myvalidator "a_plus_station/validator"
	"fmt"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/locales/en"
	"github.com/go-playground/locales/zh"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	en_translations "github.com/go-playground/validator/v10/translations/en"
	zh_translations "github.com/go-playground/validator/v10/translations/zh"
	"go.uber.org/zap"
	"reflect"
	"strings"
)

func InitTrans(locale string) (err error) {
	// 修改gin 框架中的validator引擎属性，实现定制语言
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		// 注册一个获取json的tag的自定义方法
		v.RegisterTagNameFunc(func(field reflect.StructField) string {
			name := strings.SplitN(field.Tag.Get("json"), ",", 2)[0]
			if name == "-" {
				return ""
			}
			//fmt.Println(name)
			return name
		})
		zhT := zh.New() // 中文翻译
		enT := en.New() // 英文翻译
		uni := ut.New(enT, zhT)
		global.Trans, ok = uni.GetTranslator(locale)
		if !ok {
			return fmt.Errorf("uni.err:%s", locale)
		}

		switch locale {
		case "en":
			en_translations.RegisterDefaultTranslations(v, global.Trans)
		case "zh":
			zh_translations.RegisterDefaultTranslations(v, global.Trans)
		default:
			en_translations.RegisterDefaultTranslations(v, global.Trans)
		}
		return
	}
	return
}

func InitMyValidate() (err error) {
	// 注册一个validator验证器
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {

		//布尔值验证
		err = v.RegisterValidation("boolean", myvalidator.ValidateBool)
		if err != nil {
			zap.S().Panicf("注册validator 失败 %s", err.Error())
		}
		// mobile 验证失败的返回信息
		err = v.RegisterTranslation("boolean", global.Trans, func(ut ut.Translator) error {
			return ut.Add("boolean", "{0} 非法的布尔值", true)
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("boolean", fe.Field())
			return t
		})

		//邮箱验证
		err = v.RegisterValidation("email", myvalidator.ValidateEmail)
		if err != nil {
			zap.S().Panicf("注册validator 失败 %s", err.Error())
		}
		// mobile 验证失败的返回信息
		err = v.RegisterTranslation("email", global.Trans, func(ut ut.Translator) error {
			return ut.Add("email", "{0} 非法的邮箱", true)
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("email", fe.Field())
			return t
		})

		// 手机韩验证器
		err := v.RegisterValidation("mobile", myvalidator.ValidateMobile)
		if err != nil {
			zap.S().Panicf("注册validator 失败 %s", err.Error())
		}
		// mobile 验证失败的返回信息
		err = v.RegisterTranslation("mobile", global.Trans, func(ut ut.Translator) error {
			return ut.Add("mobile", "{0} 非法的手机号码", true)
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("mobile", fe.Field())
			return t
		})

		// gender 验证
		err = v.RegisterValidation("gender", myvalidator.ValidateGender)
		if err != nil {
			zap.S().Panicf("注册validator 失败 %s", err.Error())
		}
		// mobile 验证失败的返回信息
		err = v.RegisterTranslation("gender", global.Trans, func(ut ut.Translator) error {
			return ut.Add("gender", "{0} 非法的性别", true)
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("gender", fe.Field())
			return t
		})

		// 文件后缀 验证
		err = v.RegisterValidation("fileSuffix", myvalidator.ValidateFileSuffix)
		if err != nil {
			zap.S().Panicf("注册validator 失败 %s", err.Error())
		}
		// mobile 验证失败的返回信息
		err = v.RegisterTranslation("fileSuffix", global.Trans, func(ut ut.Translator) error {
			return ut.Add("fileSuffix", "{0} 非法的后缀", true)
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("fileSuffix", fe.Field())
			return t
		})

		// 头像url 验证
		err = v.RegisterValidation("imageUrl", myvalidator.ValidateImage)
		if err != nil {
			zap.S().Panicf("注册validator 失败 %s", err.Error())
		}
		// mobile 验证失败的返回信息
		err = v.RegisterTranslation("imageUrl", global.Trans, func(ut ut.Translator) error {
			return ut.Add("imageUrl", "{0} 非法URL", true)
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("imageUrl", fe.Field())
			return t
		})
	}
	return
}
